﻿namespace EnterpriseLibrary.Caching.AzureBackingStore.Configuration
{
    using System.Collections.Generic;
    using System.Configuration;

    using Microsoft.Practices.EnterpriseLibrary.Caching;
    using Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations;
    using Microsoft.Practices.EnterpriseLibrary.Caching.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;

    [ResourceDescription(typeof(DesignResources), "AzureCacheStorageDataDescription")]
    [ResourceDisplayName(typeof(DesignResources), "AzureCacheStorageDataDisplayName")]
    [System.ComponentModel.Browsable(true)]
    public class AzureCacheStorageData : CacheStorageData
    {
        #region Fields

        private const string DataCacheNameProperty = "dataCacheName";
        private const string DataCacheRegionNameProperty = "dataCacheRegionName";

        #endregion Fields

        #region Constructors

        // private const string DefaultExpirationTimeInMSProperty = "defaultExpirationTimeInMS";
        public AzureCacheStorageData()
            : base(typeof(AzureBackingStore))
        {
        }

        public AzureCacheStorageData(string name, string dataCacheName)
            : base(name, typeof(AzureBackingStore))
        {
            this.DataCacheName = dataCacheName;
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Name of the database instance to use for storage. Instance must be defined in Data configuration.
        /// </summary>
        [ConfigurationProperty(DataCacheNameProperty, IsRequired = true)]
        [ResourceDescription(typeof(DesignResources), "AzureCacheStorageDataCacheNameDescription")]
        [ResourceDisplayName(typeof(DesignResources), "AzureCacheStorageDataCacheNameDisplayName")]
        public string DataCacheName
        {
            get { return (string)base[DataCacheNameProperty]; }
            set { base[DataCacheNameProperty] = value; }
        }

        /// <summary>
        /// Name of the database instance to use for storage. Instance must be defined in Data configuration.
        /// </summary>
        [ConfigurationProperty(DataCacheRegionNameProperty, IsRequired = true)]
        [ResourceDescription(typeof(DesignResources), "AzureCacheStorageDataCacheRegionNameDescription")]
        [ResourceDisplayName(typeof(DesignResources), "AzureCacheStorageDataCacheRegionNameDisplayName")]
        public string DataCacheRegionName
        {
            get { return (string)base[DataCacheRegionNameProperty]; }
            set { base[DataCacheRegionNameProperty] = value; }
        }

        #endregion Properties

        #region Methods

        ///// <summary>
        ///// Name of the database instance to use for storage. Instance must be defined in Data configuration.
        ///// </summary>
        //[ConfigurationProperty(DataCacheNameProperty, IsRequired = true)]
        //[ResourceDescription(typeof(DesignResources), "AzureCacheStorageDefaultExpirationTimeInMSDescription")]
        //[ResourceDisplayName(typeof(DesignResources), "AzureCacheStorageDefaultExpirationTimeInMSDisplayName")]
        //public int DefaultExpirationTimeInMS
        //{
        //    get { return (int)base[DefaultExpirationTimeInMSProperty]; }
        //    set { base[DefaultExpirationTimeInMSProperty] = value; }
        //}
        public override IEnumerable<TypeRegistration> GetRegistrations()
        {
            yield return
                new TypeRegistration<IBackingStore>(
                    () =>
                    new AzureBackingStore(
                        this.DataCacheName, this.DataCacheRegionName, Container.ResolvedIfNotNull<IStorageEncryptionProvider>(this.StorageEncryption)))
                    {
                Name = this.Name
            };
        }

        #endregion Methods
    }
}